----------------------------------------------------------------------------------------------------------------------------------
--[[

    用来修改 function Sanity:Recalc(dt)  里的  self.custom_rate_fn(inst,dt)  的值,实现各种自定义的理智回复速度

    加法控制    

]]--
----------------------------------------------------------------------------------------------------------------------------------
local hutao_com_sanity_custom_rate = Class(function(self, inst)
    self.inst = inst

    
    self.additive_controllers = {} -- 加法控制器
    self.additive_controller_remove_event_flags = {} -- 加法控制器移除标记
    self.___additive_controller_remove_event_fn = function(tempInst)
        self:RemoveRate(tempInst)
    end

    inst:DoTaskInTime(0,function(inst)
        if inst.components.sanity then
            local old_rate_fn = inst.components.sanity.custom_rate_fn or function()
                return 0
            end
            inst.components.sanity.custom_rate_fn = function(_,dt)
                return self:GetValue() + old_rate_fn(_,dt)
            end
        end
    end)

end,
nil,
{

})
------------------------------------------------------------------------------------------------------------------------------
---
    function hutao_com_sanity_custom_rate:AddRate(tempInst,value)
        self.additive_controllers[tempInst] = value
        if not self.additive_controller_remove_event_flags[tempInst] then
            self.additive_controller_remove_event_flags[tempInst] = true
            tempInst:ListenForEvent("onremove",self.___additive_controller_remove_event_fn)
        end
    end
    function hutao_com_sanity_custom_rate:RemoveRate(tempInst)
        local new_table = {}
        for k,v in pairs(self.additive_controllers) do
            if k ~= tempInst then
                new_table[k] = v
            end
        end
        self.additive_controllers = new_table
        if self.additive_controller_remove_event_flags[tempInst] then
            self.additive_controller_remove_event_flags[tempInst] = nil
            tempInst:RemoveEventCallback("onremove",self.___additive_controller_remove_event_fn)
        end
    end
------------------------------------------------------------------------------------------------------------------------------
-- 
    function hutao_com_sanity_custom_rate:Clear()
        for k,v in pairs(self.additive_controllers) do
            if k and k:IsValid() then
                self:RemoveRate(k)
            end
        end
    end
------------------------------------------------------------------------------------------------------------------------------
--- 每分钟回复 .  TUNING.DAPPERNESS_SMALL -- 16.0 per day. a day is 8 min
    function hutao_com_sanity_custom_rate:AddRatePerMin(tempInst,value)
        self:AddRate(tempInst,TUNING.DAPPERNESS_SMALL/16*value)
    end
    function hutao_com_sanity_custom_rate:AddRatePerDay(tempInst,value)
        self:AddRate(tempInst,TUNING.DAPPERNESS_SMALL/2*value)
    end
------------------------------------------------------------------------------------------------------------------------------
---
    function hutao_com_sanity_custom_rate:GetValue()
        local value = 0
        for k,v in pairs(self.additive_controllers) do
            if k and k:IsValid() then
                value = value + v
            end
        end
        -- print("hutao_com_sanity_custom_rate:GetValue",value)
        return value
    end
------------------------------------------------------------------------------------------------------------------------------
return hutao_com_sanity_custom_rate







